package com.vastio.boot.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author hjwStart
 * @time 2022-12-01 11:31
 * @version: 1.0
 */
@Controller
public class MyController {

    @PostMapping("/login")
    public ModelAndView index(String userName, String userPwd) {
        Subject currentUser = SecurityUtils.getSubject();
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(userName, userPwd);
            currentUser.login(token);
            return new ModelAndView("index");
        }
        return new ModelAndView("login");
    }

    @GetMapping(value = {"/hello"})
    @ResponseBody
    public String hello() {
        return "Hello World";
    }

    @GetMapping(value = "/toLogin")
    public ModelAndView toLogin() {
        return new ModelAndView("login");
    }
}
